package sales.managedbean;

import company.entity.Company;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.io.Serializable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import sales.entity.LineItem;
import sales.entity.Product;
import sales.entity.SalesQuotation;
import sales.seesion.stateless.PriceManageSessionBean;
import sales.seesion.stateless.ConvertManageSessionBean;
import sales.seesion.stateless.SalesQuotationSessionBean;
import sales.util.exception.SalesException;

@ManagedBean(name = "createNewQuotationManagedBean")
@ViewScoped
public class CreateNewQuotationManagedBean implements Serializable {

    @EJB
    SalesQuotationSessionBean salesQuotationSessionBean;
    @EJB
    ConvertManageSessionBean convertManageSessionBean;
    @EJB
    PriceManageSessionBean priceManageSessionBean;
    //@EJB SalesInquirySessionBean
    private SalesQuotation salesQuotation;
    private LineItem lineItem;
    private List<LineItem> lineItemList;
    //private  Long SalesInquiry salesInquiryId;
    private String productName;
    private String productCode;
    private Long companyId;
    private Company selectedCompany;
   

    public CreateNewQuotationManagedBean() {
        lineItem = new LineItem();
        lineItemList = new ArrayList<LineItem>();
        salesQuotation=new SalesQuotation();

    }

    public void addLineItem(ActionEvent event) {
        boolean flag=false;
        //productCode=productManageSessionBean.productNameCodeConvert(productName);
       Product product = salesQuotationSessionBean.getProduct(productName);
        
        // Product product=salesQuotationSessionBean.getProductByCode(productCode);
        
        for(int i=0;i<getLineItemList().size();i++){
           if(getLineItemList().get(i).getProduct().getProductName().equals(productName))
           {FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Product has already been added.", ""));        
            flag=true;
           break;
           }
        }
        if(flag==false)
        {lineItem.setProduct(product);
            getLineItemList().add(lineItem);}
        lineItem = new LineItem();
    }

   public void deleteLineItem(LineItem lineItem) {
       getLineItemList().remove(lineItem);
    }
//     public void deleteLineItem(ActionEvent event) {
//         lineItem = (LineItem) event.getComponent().getAttributes().get("o");
//        getLineItemList().remove(lineItem);
//    }

    public void submitQuotation(ActionEvent event) throws IOException, ParseException,SalesException {
        int quantity = 0;
        String companyName = "";
        String companyType="";
        //String companyTypeCode = "";
        double returnedPrice = 0.0;
        int leadTime = 0;
        Date validUntil=new Date();

        //pass the productType+quantity to pricecheck function & product function to return unit price&totalprice&LeadTime&validTime
        companyName = (String) event.getComponent().getAttributes().get("companyName");
        selectedCompany = salesQuotationSessionBean.getCompanyByName(companyName);
       // System.out.print("nnnnnnnnnnname"+ selectedCompany.getName());
        
       // companyTypeCode = (String) convertManageSessionBean.companyTypeNameCodeConvert(selectedCompany.getCompanyType());
        //have to deal with empty lineItemList first!
        for (int i = 0; i < getLineItemList().size(); i++) { //for each lineItem, return a after-discount price+ lead time
             //!!! productCode=(String)convertManageSessionBean.productNameCodeConvert(lineItemList.get(i).getProduct().getProductName());

            quantity = getLineItemList().get(i).getQuantity();
            returnedPrice = priceManageSessionBean.calculatePirce(selectedCompany.getCompanyType(), getLineItemList().get(i).getProduct(), quantity);
            leadTime = 30; //hardCode-> leadTime=productManageSessionBean.calculateLeadTime(...);
            getLineItemList().get(i).setPrice(returnedPrice);
            getLineItemList().get(i).setLeadTime(leadTime);
     }
        salesQuotation.setCompany(getSelectedCompany());
        salesQuotation.setQuotationLineItems(getLineItemList());
        
        //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedCompany", getSelectedCompany());
        //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lineItemList", getLineItemList());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("quotation", getSalesQuotation());
        FacesContext.getCurrentInstance().getExternalContext().redirect("quotationForConfirm.xhtml");
    }

//    public void saveNewQuotation(ActionEvent event) throws IOException {
//       
//    }
    
//    

    public SalesQuotation getSalesQuotation() {
        return salesQuotation;
    }

    public void setSalesQuotation(SalesQuotation salesQuotation) {
        this.salesQuotation = salesQuotation;
    }

    public LineItem getLineItem() {
        return lineItem;
    }

    public void setLineItem(LineItem lineItem) {
        this.lineItem = lineItem;
    }

    public List<LineItem> getLineItemList() {
        return lineItemList;
    }

    public void setLineItemList(List<LineItem> lineItemList) {
        this.setLineItemList(lineItemList);
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }
    
    public Company getSelectedCompany(){
      return this.selectedCompany;
    }
    
    public void setSelectedCompany(Company company){
        this.selectedCompany=company;
    }
}
